﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HDTuyenChon.AppCode;
using System.Web.UI.WebControls;

namespace HDTuyenChon.BL
{
	public class CommonBL
	{
		public ListItem[] GetComboData(string tenItem)
		{
			ListItem[] comboItems = null;

			try
			{
				using (AppCode.HDFilmDataContext db = new AppCode.HDFilmDataContext())
				{

					//Select du lieu
					if (tenItem.Equals("NAM_SX"))
					{
						var comboData = (from itm in db.Mstitems
										 join itmName in db.Mstitemnames on itm.Id equals itmName.ItemID
										 where itm.TenItem == tenItem
										 orderby itmName.ItemName descending
										 select itmName).ToList();
						//Set vao ListItem
						if (comboData != null && comboData.Count > 0)
						{
							comboItems = new ListItem[comboData.Count];
							for (int i = 0; i < comboItems.Length; i++)
							{
								Mstitemname itm = comboData[i];
								comboItems[i] = new ListItem(itm.ItemName, itm.ItemValue);
							}
						}
					}
					else
					{
						var comboData = (from itm in db.Mstitems
										 join itmName in db.Mstitemnames on itm.Id equals itmName.ItemID
										 where itm.TenItem == tenItem
										 orderby itmName.ItemName
										 select itmName).ToList();
						//Set vao ListItem
						if (comboData != null && comboData.Count > 0)
						{
							comboItems = new ListItem[comboData.Count];
							for (int i = 0; i < comboItems.Length; i++)
							{
								Mstitemname itm = comboData[i];
								comboItems[i] = new ListItem(itm.ItemName, itm.ItemValue);
							}
						}
					}
				}
			}
			catch (Exception exp)
			{
				throw new Exception("CommonBL.cs - GetComboData - " + exp.Message.ToString(), exp);
			}
						
			return comboItems;
		}
	}
}